package com.cn.listen;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cn.entity.CustomerInfo;
import com.cn.service.CustomerInfoService;
import com.cn.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class ExcelListener extends AnalysisEventListener {

    private CustomerInfoService customerInfoService;

    private List<Object>  data = new ArrayList<Object>();

    private String sign;

    public ExcelListener() {
    }

    public ExcelListener(CustomerInfoService customerInfoService) {
        this.customerInfoService=customerInfoService;
    }
    public ExcelListener(CustomerInfoService customerInfoService, String sign) {
        this.customerInfoService=customerInfoService;
        this.sign=sign;
    }

    @Override
    public void invoke(Object object, AnalysisContext context) {
//        System.out.println(context.getCurrentSheet());
        data.add(object);
        if(data.size()>=1000){
            doSomething();
            data = new ArrayList<Object>();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        doSomething();
    }

    /**
     * 绩效考核.xlsx
     */
    public void doSomething(){

        switch (sign){
            case "jixiaokaohe":
                jixiaokaohe();
                break;
            case "jiangsujixiaokaohe":
                jiangsujixiaokaohe();
                break;
            case "hunan":
                hu_nan();
                break;
        }

    }
    private void hu_nan(){
        List<String> obj =null;
        CustomerInfo customerInfo=null;
        List<CustomerInfo> list= new ArrayList<>();
        for (Object o:data) {
            try{
                obj = (List)o;
                if(obj.size()==7){
                    customerInfo=new CustomerInfo();
                    customerInfo.setLicense_key(obj.get(0));
                    customerInfo.setContacts(obj.get(1));
                    customerInfo.setShop_name(obj.get(2));
                    customerInfo.setAddress(obj.get(3));

//                    customerInfo.setIndustry_status(obj.get(4));

                    String [] phones= StringUtil.isNotempty(obj.get(4))?obj.get(4).split(","):new String[0];
//                    if(phones.length==3){
                    setPhones(phones,customerInfo);
//                    }else if(phones.length==2){
//                        setPhones(phones,customerInfo);
//                    }else{
//                        customerInfo.setPhone(obj.get(4));
//                    }

//                    customerInfo.setIscity(obj.get(5));
//                    customerInfo.setScale(obj.get(6));

                    customerInfo.setCity(obj.get(6));
                    list.add(customerInfo);
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        try {
            customerInfoService.saveBatch(list);
            System.out.println("list->"+list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setPhones(String [] phones,CustomerInfo customerInfo){
        int i=0;
        for (String s:phones) {
            if(StringUtil.isNotempty(s)){
                if(s.length()==8){
                    customerInfo.setTel(s);
                }
                if(s.length()==11){
                    switch (i){
                        case 0:
                            customerInfo.setPhone(s);
                            break;
                        case 1:
                            customerInfo.setPhone1(s);
                            break;
                        case 2:
                            customerInfo.setPhone2(s);
                            break;
                    }
                    i++;
                }
            }
        }
    }

    private void jiangsujixiaokaohe(){
        List<String> obj =null;
        CustomerInfo customerInfo=null;
        List<CustomerInfo> list= new ArrayList<>();
        for (Object o:data) {
            try{
                obj = (List)o;
//                System.out.println(o);
                if(obj.size()==8){
                    customerInfo=new CustomerInfo();
//                    customerInfo.setLicense_key(obj.get(0));
                    customerInfo.setShop_name(obj.get(1));
                    customerInfo.setAddress(obj.get(2));
                    customerInfo.setContacts(obj.get(3));
                    customerInfo.setIndustry_status(obj.get(4));
                    customerInfo.setIscity(obj.get(5));
                    customerInfo.setScale(obj.get(6));
                    customerInfo.setPhone(obj.get(7));
//                    customerInfo.setCity(obj.get(8));
                    list.add(customerInfo);
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        try {
            customerInfoService.saveBatch(list);
            System.out.println("list->"+list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jixiaokaohe(){
        List<String> obj =null;
        CustomerInfo customerInfo=null;
        List<CustomerInfo> list= new ArrayList<>();
        for (Object o:data) {
            try{
                obj = (List)o;
//                System.out.println(o);
                if(obj.size()==9){
                    customerInfo=new CustomerInfo();
                    customerInfo.setAddress(obj.get(2));
                    customerInfo.setLicense_key(obj.get(0));
                    customerInfo.setShop_name(obj.get(1));
                    customerInfo.setContacts(obj.get(3));
                    customerInfo.setIndustry_status(obj.get(4));
                    customerInfo.setIscity(obj.get(5));
                    customerInfo.setScale(obj.get(6));
                    customerInfo.setPhone(obj.get(7));
                    customerInfo.setCity(obj.get(8));
                    list.add(customerInfo);
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        try {
            customerInfoService.saveBatch(list);
            System.out.println("list->"+list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
